home *** CD-ROM | disk | FTP | other *** search
- Path: news.lpr.carel.fi!usenet
- From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
- Newsgroups: comp.lang.c
- Subject: Re: Pointers to register
- Date: Tue, 19 Mar 1996 13:22:37 +0200
- Organization: Carelcomp Products
- Message-ID: <314E98FD.5722@cmt.lpr.mail.carel.fi>
- References: <1239@altheim.win-uk.net>
- NNTP-Posting-Host: renoir.cclahti.carel.fi
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- Brian R. Oldham wrote:
- >
- > A couple of weeks ago someone posted the opinion that all objects in
- > memory must have an address, which might have gone uncontested but for
- > the fact that he added that therefore all pointers must point to an
- > address. Someone else reminded us that registers don't have an address.
- >
- > Bearing in mind the usual way to assign a pointer:
- >
- > ptr = &var;
- >
- > is correct for objects in memory, but how do you assign a pointer
- > to a register?
- >
- > The following function (a getch() for x86) works: But is it right??
- >
- > /* Read next keystroke */
- > #include <dos.h>
- >
- > #define KEYBD 0x16
- >
- > static union REGS inregs, outregs;
- >
- > void getkey(int *scancode, char *ch)
- > {
- > inregs.h.ah = 0x00;
- > int86(KEYBD,&inregs,&outregs);
- > *scancode = outregs.h.ah; /* ??? */
- > *ch = outregs.h.al;
- > }
- >
-
- You have things a little confused here. The "inregs" and "outregs" used in a call to
- "int86" are structures containing images of the registers. Register, as what your posting
- implies to, are the CPU's internal storages for data, such as AX, BX, SI, etc. In a
- standard C program, you cannot access these directly - the use of them is
- implementation-dependant. However, you can suggest the compiler to use registers by
-
- void function(void)
- {
- register int xyz;
- ...
- }
-
- or something like that. It still doesn't mean that the compiler will actually place xyz
- in a register - it's just a suggestion you made. Current compilers tend to put variables
- into registers whenever possible (so called automatic vars) to speed things up, but the
- only way to see that is to take look at the compiled code. However, in a situation like
- the next one, the address of a register variable (if it gets to put in a register) is
- never needed, so the compiler may tend to optimize it out (agreed, this is a stupid
- example):
-
- void function(void)
- {
- register int xyz;
- int *p = &xyz;
-
- *p = 0;
- }
-
- OTOH, if the compiler does _not_ optimize this out, it will neither put xyz in a
- register, because the address of it is needed.
-
- Hope this sheds some light.
-
- Later,
- AriL
- --
- All my opinions are mine and mine alone.
-